<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <!-- tab栏 -->
        <el-tabs v-model="activeName">
          <!-- 面板1 -->
          <el-tab-pane v-loading="loading" label="角色管理" name="role">
            <!-- 按钮 -->
            <el-button type="primary" size="small" icon="el-icon-plus" @click="clickShowDialog">添加角色</el-button>
            <!-- 表格 -->
            <el-table :border="true" :data="tableData">
              <el-table-column type="index" :index="indexMethod" label="序号" width="100" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEditRoleDialog(row.id) ">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4, 5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息" name="company">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-dialog :title="showTitle" :visible="showDialog" :before-close="closeDialog">
            <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入角色名称" />
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input v-model="form.description" placeholder="请输入角色描述" />
              </el-form-item>
            </el-form>

            <template #footer>
              <el-button @click="closeDialog">取消</el-button>
              <el-button type="primary" @click="clickSubmit">确认</el-button>
            </template>
          </el-dialog>

        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqDeleteRole, reqGetRoleList, reqAddRole, reqGetRoleDetail, reqUpdateRole } from '@/api/setting'
import { reqGetCompanyById } from '@/api/company'
import { mapState } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      // 绑定激活的tab栏的name
      activeName: 'role',
      tableData: [],
      page: 1,
      pagesize: 3,
      total: 0, // 总条数
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
        ]
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapState('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompany()
  },
  methods: {
    // 获取公司信息
    async  getCompany() {
      const res = await reqGetCompanyById(this.userInfo.companyId)
      this.companyForm = res.data
    },
    // 获取角色列表，发送请求获取数据
    async getRoleList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetRoleList(this.page, this.pagesize)
      this.tableData = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 更新煤业条数
      this.pagesize = val
      // 重置当前页
      this.page = 1
      // 调用方法, 重新请求
      this.getRoleList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      // 更新当前页
      this.page = val
      // 调用方法，重新请求渲染
      this.getRoleList()
    },
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    delRole(id) {
      // 1 提示框
      this.$confirm('亲，你确定要离开吗？', '温馨提示').then(async() => {
        // 2 确认发送请求
        await reqDeleteRole(id)
        this.$message.success('删除成功')
        // 如果删除的是当前仅有的的最后一条，那么页码就需要发生变化（让当前页-1）
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        // 冲新渲染当前页
        this.getRoleList
      }).catch(() => {
        console.log('取消')
      })
    },
    clickShowDialog() {
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      // 重置表单数据
      this.$refs.roleForm.resetFields()
      // 重置内容
      this.form = {
        name: '',
        description: ''
      }
    },
    clickSubmit() {
      // 先校验
      this.$refs.roleForm.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          // 发送编辑请求
          await reqUpdateRole(this.form)
        } else {
          // 发送添加请求
          await reqAddRole(this.form)
        }
        // // 关闭弹框
        this.closeDialog()

        // 添加成功
        this.$message.success('操作成功')
        // 重新渲染
        this.getRoleList()
      })
    },
    async  showEditRoleDialog(id) {
      this.showDialog = true
      // 根据id发送请求进行回显
      const { data } = await reqGetRoleDetail(id)
      this.form = data
    }

  }
}
</script>

<style>

</style>
